<!--# layout("/common/layout.html",{"jsBase":"/js/customer/member/"}){ -->
<center>
<form class="layui-form layui-form-pane" id="addForm" enctype="multipart/form-data"  lay-filter="editform" style="vertical-align:middle">
    <input type="hidden" name="customerId" id="customerId">
    <input type="hidden" name="tenantId" id="tenantId">
    <input type="hidden" name="choiceMode" id="choiceMode">
    <br>

    <div class="layui-row">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">高考年份</label>
                <div class="layui-input-inline">
                    <input type="text" name="choiceYear" id="choiceYear" autocomplete="off" class="layui-input" lay-verify="number">
                </div>
            </div>

        </div>
    </div>
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">高考省份</label>
                <div class="layui-input-inline">
                    <layui:simpleDataSelect name="provinceId"  value="" data="${provList}"
                                            attrId="provId" attrName="provName" layFilter="colFilter" id="provinceId"  laySearch="true"/>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline" id="majorTypeDiv">
                <label class="layui-form-label">高考科目</label>
                <div class="layui-input-inline">
                    <input type="radio" name="majorType" value="1" title="文科">
                    <input type="radio" name="majorType" value="2" title="理科" checked>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline" id="courseGroupIdDiv">
                <label class="layui-form-label">高考科目</label>
                <div class="layui-input-inline" id="courseGroupDiv" style="float:left">

                </div>
            </div>

        </div>
    </div>
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">高考分数</label>
                <div class="layui-input-inline">
                    <input type="text" name="score" id="score" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <div class="layui-row">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">高考排名</label>
                <div class="layui-input-inline">
                    <input type="text" name="rank" id="rank" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
    </div>
    <layui:submitButtons id="addButton" />
</form>
</center>

<!--#} -->
<script>
    layui.define([ 'form', 'laydate', 'table','memberApi'], function(exports) {
        var form = layui.form;
        var memberApi = layui.memberApi;
        var index = layui.index;
        var laydate = layui.laydate;
        var isNew = true;
        var choiceMode = 0;
        var reformYear = 0;
        var valueGroup ="${customerInfo.valueGroup}";
        <!--# if(!(isEmpty(customerInfoJson))){  -->
        isNew = false;
        <!--# } -->
        $("#courseGroupIdDiv").hide();
        var view = {
            init:function(){
                Lib.initGenrealForm($("#addForm"),form);
                this.initSubmit();
            },
            initSubmit:function(){
                $("#addButton").click(function(){
                    var length = $("input[type='checkbox']:checked").length;
                    $("#choiceMode").val(choiceMode);
                    var majorType = "";
                    var subject = "";
                    if(choiceMode!=0){
                        if(length>3){
                            Common.info("只能选择三个科目");
                            return;
                        }else if(length<3){
                            Common.info("请选择三个科目");
                            return;
                        }

                        $("input[type='checkbox']:checked").each(function(){
                            var title = $(this).attr('title');
                            subject += title+"/";
                        });
                        subject = subject.substring(0,subject.length-1);
                        $("input[name='majorType'][value=3]").attr("checked",true);
                    }else{
                        majorType = $("input[name='majorType'][checked]").val();
                    }
                    var choiceYear = $("#choiceYear").val();
                    var provName = $("#provinceId option:selected").text()
                    var score = $("#score").val();
                    var  rank = $("#rank").val();
                    if(choiceYear==""){
                        Common.info("请填写高考年份");
                        return;
                    }
                    var date  = new Date();
                    var dateYear = date.getFullYear();
                    if(choiceYear>dateYear+2){
                        Common.info("请填写正确的高考年份");
                        return;
                    }
                    if($("#provinceId").val()==""||$("#provinceId").val()==0){
                        Common.info("请填选择高考省份");
                        return;
                    }
                    form.on('submit(form)', function(){
                        memberApi.saveInfo($('#addForm'),function(){
                            // parent.window.dataReload();
                            parent.ChooseAdidValues(majorType,subject,choiceYear,provName,score,rank,choiceMode);
                            Common.info("保存成功");
                            Lib.closeFrame();
                        });
                    });
                });
                $("#addButton-cancel").click(function(){
                    Lib.closeFrame();
                });
            }

        }

        baseData(${customerInfo.provinceId},true);
        form.on('select(colFilter)', function(data){
            baseData(data.value,false);
        });
        //获取高考模式信息
        function baseData(provinceId,flag){
            $.ajax({
                type: 'POST',
                url: "${ctxPath}/customer/customerInfo/getBaseData.json?",
                data: {
                    provinceId:provinceId
                },
                dataType:'json',
                success: function(res){
                    if(res.code==0){
                        operaData(res.data,flag);
                    }
                }
            });
        }

        $("#choiceYear").blur(function(){
            var provinceId = $("#provinceId").val();
            if(provinceId!=""&&provinceId!=0){
                baseData(provinceId,false);
            }
        });

        //处理数据
        function operaData(data,flag){
            choiceMode = data.choiceMode;
            reformYear = data.reformYear==null?0:data.reformYear;
            var year = $("#choiceYear").val();
            if(year >= reformYear){
                choiceMode = 1;
            }
            if(choiceMode==0){
                $("#courseGroupIdDiv").hide();
                $("#majorTypeDiv").show();
                $("#courseGroupDiv").html("");
                form.render();
            }else{
                $("#courseGroupIdDiv").show();
                $("#majorTypeDiv").hide();
                var array =  eval(data.choiceSubjects);
                var html = "";
                for(var i in array){
                    html+="<input type='checkbox' name='courseGroupId' title='"+array[i].name+"'  value='"+array[i].id+"' lay-skin='primary' >";
                }
                $("#courseGroupDiv").html(html);
                if(flag){
                    setCheckBox();
                }
                form.render();
            }
        }

        function setCheckBox(){
            $("input[type='checkbox']").each(function(){
                var split = valueGroup.split(",");
                var value = $(this).attr('value');
                for(var i in split){
                    if(split[i]==value){
                        $(this).attr('checked',true);
                        return;
                    }
                }
            });
            form.render();
        }

        view.init();
        <!--# if(!(isEmpty(customerInfoJson))){  -->
        var formJson = ${customerInfoJson};
        form.val("editform",formJson);
        $(".image-preview").each(function() {
            $(this).attr("src",formJson[$(this).attr("id")]);
        });
        <!--# } -->
    });
</script>
